/**
 * com.chaeron.GPS - An API for access to GPS devices.
 *
 *
 * Copyright (c) 2001, 2002 Chaeron Corporation,
 * All rights reserved.
 * 
 * Chaeron grants you a non-exclusive license to use, modify and re-distribute
 * this program under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version, provided that this copyright notice and 
 * license appear on all copies of the software.
 * 
 * Software is provided "AS IS," without a warranty of any kind. ALL EXPRESS OR
 * IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. THE ENTIRE RISK ARISING OUT OF USING THE SOFTWARE IS ASSUMED
 * BY THE LICENSEE. See the GNU General Public License for more details.
 *
 * CHAERON AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR DISTRIBUTING SOFTWARE.
 * IN NO EVENT WILL CHAERON OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
 * PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL
 * OR PUNITIVE DAMAGES, HOWEVER CAUSED AN REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF CHAERON HAS
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 * @version     1.02
 * @author      Andrzej Jan Taramina, Chaeron Corporation
 *              andrzej@chaeron.com
 */

package com.chaeron.GPS.NMEA;

//***************************************************************************
//*
//*     Imports
//*
//***************************************************************************/

import com.chaeron.GPS.*;
import com.chaeron.GPS.event.*;
import com.chaeron.GPS.exception.*;


//***************************************************************************
//*
//*     HDGSentence Class definition
//*
//***************************************************************************/

/**
 * The RMCSentence class represents NMEA HDG - Heading, Deviation & Variation
 *
 */

public class HDGSentence extends NMEASentence implements GPSHeading
{
	//***************************************************************************
	//*
	//*     Constant definitions
	//*
	//***************************************************************************/
	
	private final static int		HDG_NUM_FIELDS				= 5;
	
	
	//***************************************************************************
	//*
	//*     Attributes
	//*
	//***************************************************************************/
	
	private float		heading					= 0.0f;
	private float		deviation				= 0.0f;
	private float		variation				= 0.0f;
	
	
	//***************************************************************************
	//*
	//*     Constructors
	//*
	//***************************************************************************/
	
	/**
	 * Create a HDGSentence from the given packet data
	 *
	 * @param	tid			talker ID
	 * @param	sid			Sentence ID
	 * @param	sentenceStr	sentence string buffer
	 */
	
	HDGSentence( int tid, int sid, String sentenceStr )
	{  
		super( tid, sid, sentenceStr );
		
		setFieldCount( HDG_NUM_FIELDS );	
	}
	
	
	//***************************************************************************
	//*
	//*     Methods
	//*
	//***************************************************************************/
	
	/**
	 * Decode the data fields for a HDG sentence
	 *
	 * @return		This method will return a standard error code or GPS.ERROR_OK
	 * @throws GPSPacketException
	 * @throws GPSFunctionUnavailableException
	 */
	
	public void parseData() throws GPSPacketException, GPSFunctionUnavailableException
	{ 
		int		i;
		
		if( getFieldCount() >= HDG_NUM_FIELDS ) {
			heading				= getNMEAFloat( getField( 0 ) );					// Magnetic Heading (degrees)			
			deviation			= getNMEADegrees( getField( 1 ), getField( 2 ) );	// Magnetic Deviation (degrees)			
			variation			= getNMEADegrees( getField( 3 ), getField( 4 ) );	// Magnetic Variation (degrees)			
		} else {
			throw( new GPSPacketException( GPSPacketException.EXCEPTION_NUMFIELDS_INVALID ) );
		}
	}
	
	
	//***************************************************************************
	//*
	//*     Sentence Processing Methods
	//*
	//***************************************************************************/
	
	/**
	 * Process the sentence
	 *
	 * @param	gps			the GPS object that requested the HDG sentence to be processed
	 */
	
	void process( GPS gps )
	{
		gps.postGPSEvent( new GPSEvent( GPSEvent.GPS_EVENT_HEADING, this ) );
	}
	
	
	/**
	 * Get events generated.
	 *
	 * @return		return a valid event type(s) that will be generated by this sentence subclass
	 */
	
	static int getEventsGenerated()
	{  
		return( GPSEvent.GPS_EVENT_HEADING | GPSEvent.GPS_EVENT_NMEA_SENTENCE );
	}
	
	
	/**
	 * Does the object contain valid data?
	 *
	 * @return		Does the object contain valid data?
	 */
	
	public boolean isDataValid()
	{  
		return( heading != 0.0f || deviation != 0.0f || variation != 0.0f );
	}
	
	
	//***************************************************************************
	//*
	//*    Accessor methods
	//*
	//***************************************************************************/
	
	/**
	 * Get the Magnetic Heading (degrees)
	 *
	 * @return		Magnetic Heading (degrees)
	 */
	
	public float getHeading()
	{  
		return( heading );
	}
	
	
	/**
	 * Get the Magnetic Deviation (degrees)
	 *
	 * @return		Magnetic Deviation (degrees)
	 */
	
	public float getDeviation()
	{  
		return( deviation );
	}
	
	
	/**
	 * Get the Magnetic Variation (degrees)
	 *
	 * @return		Magnetic Variation (degrees)
	 */
	
	public float getVariation()
	{  
		return( variation );
	}
	
}
